'use strict';
/**--权限获取指令
**格式：<jisu-perm jisu-model="abcd"></jisu-perm>
*jisu-model为双向绑定的选定权限对象，数组格式[ID,ID,ID]
*jisu-change 档0时，触发权限筛选
**/
var app = angular.module("nspermirective", ['ngAnimate']);
app.directive("jisuPerm", function ($rootScope) {
    return {
        scope: {
            myModels: '=jisuModel'
        },
        //replace: true,
        templateUrl: "/system/directive/personnel/nspermirective.html",
        link: function ($scope, elm, attr, controller) {
            $scope.mymenu = $rootScope.rootscopeauthority; //全部菜单
            $scope.premmenulist = []; //有权限的菜单
            // $scope.myModels = [];//权限ID
            //console.info($scope.mymenu);
        },
        controller: function ($scope, $document, $http) {

            $scope.mymenu = $rootScope.rootscopeauthority; //全部菜单

            //根据权限，显示对应的权限数据
            $scope.$watch('myModels', function (n, o) {
                var newmenu = $scope.menufind(angular.copy($scope.mymenu), $scope.myModels); //筛选出有权限的菜单
                $scope.premmenulist = newmenu;
                //  console.log($scope.myModels);
            });

            $scope.premshow = function () {
                $scope.xzqshow = true;
                //点击非指定区域，关闭显示层
                $document.bind("click", function (evt) {
                    var evt = window.event ? window.event : evt,
                    target = evt.srcElement || evt.target;
                    var bt = $document[0].getElementById("jisu-selector").contains(target);
                    if (!bt) {
                        //console.info('1'+bt);
                        $scope.$apply(function () {
                            $scope.premclose();
                        });
                    }
                });
            }
            //选择器关闭方法
            $scope.premclose = function () {
                //结果显示
                $scope.xzqshow = false;
                var newmenu = $scope.menufind(angular.copy($scope.mymenu), $scope.myModels); //筛选出有权限的菜单
                $scope.premmenulist = angular.copy(newmenu);
                $document.unbind();
            }


            //自动选中双向绑定数据
            $scope.isChecked = function (id) {
                //console.log(id);
                if ($scope.myModels != undefined) {
                    return $scope.myModels.indexOf(id) >= 0;
                }
            };
            //全选,全部取消
            $scope.allSelcetion = function ($event) {
                var checkbox = $event.target;
                var checked = checkbox.checked;
                if (checked) {
                    for (var i = 0; i < $scope.mymenu.length; i++) {
                        var mid = $scope.mymenu[i];
                        $scope.xpush(mid.id);
                        $scope.diguiselect(mid.childroute);
                        $scope.diguiselect(mid.btnlist);
                    };
                } else {
                    $scope.myModels.splice(0, $scope.myModels.length);
                }

            }
            $scope.xpush = function (id) {
                //需要验证一下是否存在
                var idx = $scope.myModels.indexOf(id);
                if (idx < 0) {
                    $scope.myModels.push(id);
                }
            }
            //递归增加子集
            $scope.diguiselect = function (obj) {
                if (obj != undefined) {
                    for (var i = 0; i < obj.length; i++) {
                        var mid = obj[i];
                        $scope.xpush(mid.id);
                        $scope.diguiselect(mid.childroute);
                        $scope.diguiselect(mid.btnlist);
                    };
                }
                //console.log(obj);
            }
            //递归查找父级节点，并选中
            $scope.diguiselectpids = function (fobj, pid) {
                if (fobj != undefined && pid != undefined) {
                    for (var i = 0; i < fobj.length; i++) {
                        var dingobj = fobj[i];
                        //console.log("找："+dingobj.id+","+pid);
                        if (dingobj.id == pid) {
                            $scope.xpush(pid);
                            $scope.diguiselectpids($scope.mymenu, dingobj.parid);
                            break;
                        }
                        else {
                            $scope.diguiselectpids(dingobj.childroute, pid);
                        }
                    }
                }
            }
            //递归删除子集
            $scope.diguidel = function (obj) {
                if (obj != undefined) {
                    for (var i = 0; i < obj.length; i++) {
                        var mid = obj[i];
                        var idx = $scope.myModels.indexOf(mid.id);
                        if (idx >= 0) {
                            $scope.myModels.splice(idx, 1);
                        }
                        $scope.diguidel(mid.childroute);
                        $scope.diguidel(mid.btnlist);
                    };
                }
                //console.log(obj);
            }
            //取消一个界面
            $scope.clearItiem = function ($event, obj) {
                //取消选中项目
                var idx = $scope.myModels.indexOf(obj.id);
                if (idx >= 0) {
                    $scope.myModels.splice(idx, 1);
                }
                $scope.diguidel(obj.childroute);
                $scope.diguidel(obj.btnlist);                
                //重绘权限
                 $scope.premmenulist = $scope.menufind(angular.copy($scope.mymenu), $scope.myModels); //筛选出有权限的菜单
            }
            $scope.updateSelection = function ($event, obj) {
                var checkbox = $event.target;
                var checked = checkbox.checked;
                if (checked) {
                    //1、如果有子集选中全部子集
                    //2、如果有父级，选中全部父级
                    $scope.xpush(obj.id);
                    $scope.diguiselect(obj.childroute); //递归选中子集
                    $scope.diguiselect(obj.btnlist); //递归选中按钮
                    $scope.diguiselectpids($scope.mymenu, obj.parid); //递归选中父级
                } else {
                    var idx = $scope.myModels.indexOf(obj.id);
                    if (idx >= 0) {
                        $scope.myModels.splice(idx, 1);
                    }
                    $scope.diguidel(obj.childroute);
                    $scope.diguidel(obj.btnlist);
                }
                //console.log($scope.myModels);
            };
            //通过权限，筛选菜单
            $scope.menufind = function (menus, perm) {
                if (menus != undefined && perm != undefined) {
                    if (perm.length == 0 && perm[0] != "") {
                        return;
                    }
                    var linshishuzu = [];
                    for (var i = 0; i < menus.length; i++) {
                        var menuobj = menus[i];
                        var rongqi = {};
                        var idx = perm.indexOf(menuobj.id);
                        if (idx >= 0) {
                            if (menuobj.hasOwnProperty("childroute")) {
                                menuobj.childroute = angular.copy($scope.menufind(menuobj.childroute, perm));
                            }
                            if (menuobj.hasOwnProperty("btnlist")) {
                                menuobj.btnlist = angular.copy($scope.menufind(menuobj.btnlist, perm));
                            }
                            linshishuzu.push(menuobj);
                        }
                    }

                    return linshishuzu;

                }
            }

            //显示隐藏子集
            $scope.toggle = function (obj) {
                if (!obj.hasOwnProperty("hide") || obj.hide == false) {
                    obj.hide = true;
                    $scope.toggleicon(true);
                } else {
                    obj.hide = false;
                    $scope.toggleicon(false);
                }
            }

            $scope.toggleicon = function (obj) {
                return obj;
            }

            //查询出菜单
            //$http.get("/api/test/menu.json")
            //.success(function (response) {
            //console.log($rootScope.rootscopeauthority);

            //权限筛选
            var newmenu = $scope.menufind(angular.copy($scope.mymenu), $scope.myModels); //筛选出有权限的菜单
            $scope.premmenulist = angular.copy(newmenu);
            $scope.menuloading = true;
            //});

        }
    };

});